package remoteView;
import com.cloudgarden.resource.SWTResourceManager;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import controller.CommandsExcuter;
import controller.CommandsExcuter.AddFolderExcutor;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AddFolderBox extends org.eclipse.swt.widgets.Composite {

	{
		//Register as a resource user - SWTResourceManager will
		//handle the obtaining and disposing of resources
		SWTResourceManager.registerResourceUser(this);
	}
	
	private Label nameLabel;
	private Text nameText;
	private Label errLabel;
	private Button okButton;

	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void main(String[] args) {
		showGUI();
	}
	
	/**
	* Overriding checkSubclass allows this class to extend org.eclipse.swt.widgets.Composite
	*/	
	protected void checkSubclass() {
	}
	
	/**
	* Auto-generated method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void showGUI() {
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		AddFolderBox inst = new AddFolderBox(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public AddFolderBox(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
	}

	private void initGUI() {
		try {
			FormLayout thisLayout = new FormLayout();
			this.setLayout(thisLayout);
			this.setSize(280, 102);
			{
				errLabel = new Label(this, SWT.NONE);
				FormData errLabelLData = new FormData();
				errLabelLData.left =  new FormAttachment(0, 1000, 88);
				errLabelLData.top =  new FormAttachment(0, 1000, 38);
				errLabelLData.width = 162;
				errLabelLData.height = 14;
				errLabel.setLayoutData(errLabelLData);
				errLabel.setText("folder name is already exist!");
				errLabel.setForeground(SWTResourceManager.getColor(255, 0, 0));
				errLabel.setVisible(false);
			}
			{
				okButton = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData okButtonLData = new FormData();
				okButtonLData.left =  new FormAttachment(0, 1000, 107);
				okButtonLData.top =  new FormAttachment(0, 1000, 64);
				okButtonLData.width = 37;
				okButtonLData.height = 21;
				okButton.setLayoutData(okButtonLData);
				okButton.setText("Ok");
				okButton.setEnabled(false);
				okButton.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						okButtonWidgetSelected(evt);
					}
				});
			}
			{
				FormData nameTextLData = new FormData();
				nameTextLData.left =  new FormAttachment(0, 1000, 88);
				nameTextLData.top =  new FormAttachment(0, 1000, 19);
				nameTextLData.width = 175;
				nameTextLData.height = 14;
				nameText = new Text(this, SWT.NONE | SWT.BORDER);
				nameText.setLayoutData(nameTextLData);
				nameText.addKeyListener(new KeyListener() {
					
					@Override
					public void keyReleased(KeyEvent arg0) {
						if(nameText.getText().compareTo("")!=0)
							okButton.setEnabled(true);
						else
							okButton.setEnabled(false);
						
					}
					
					@Override
					public void keyPressed(KeyEvent arg0) {
						// TODO Auto-generated method stub
						
					}
				});
			}
			{
				nameLabel = new Label(this, SWT.NONE);
				FormData nameLabelLData = new FormData();
				nameLabelLData.left =  new FormAttachment(0, 1000, 10);
				nameLabelLData.top =  new FormAttachment(0, 1000, 19);
				nameLabelLData.width = 72;
				nameLabelLData.height = 12;
				nameLabel.setLayoutData(nameLabelLData);
				nameLabel.setText("Folder name:");
			}
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void okButtonWidgetSelected(SelectionEvent evt) {
		//excute "add folder" command
		AddFolderExcutor addFolderExcutor = CommandsExcuter.getCommand().getAddFolder();
		addFolderExcutor.setPath(nameText.getText());
		addFolderExcutor.setEmails(null);
		addFolderExcutor.setFolders(null);
		addFolderExcutor.excute();
		if(!addFolderExcutor.getRetVal()){
			errLabel.setVisible(true);
		}else{
			this.getShell().setVisible(false);
		}
	}

}
